package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.constant.PhotoType;
import com.fms.entity.*;
import com.fms.mapper.*;
import com.fms.result.ResultData;
import com.fms.service.OrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

@Service
@RequiredArgsConstructor
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    private final OrdersMapper ordersMapper;

    private final SysUserMapper sysUserMapper;

    private final OrdersHistoryMapper ordersHistoryMapper;

    private final SysDeptMapper sysDeptMapper;

    private final OrdersBoardMapper ordersBoardMapper;

    private final OrdersHardwareMapper ordersHardwareMapper;

    private final OutboundOrderMapper outboundOrderMapper;

    private final ShipmentMapper shipmentMapper;

    private final PhotoInfoMapper photoInfoMapper;
    @Override
    //流转订单
    public ResultData transferOrder(Orders orders) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, orders.getUsername());
        Long currentUserId = sysUserMapper.selectOne(wrapper).getId();
        //获取当前订单信息
        Integer originStatus = orders.getStatus();
        orders.setStatus(originStatus+1);
        orders.setUpdateDate(null);
        OrdersHistory ordersHistory = new OrdersHistory();
        //判定当前订单状态
        if (originStatus==1){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待材料信息填写");
            ordersHistory.setBrief("订单提交创建");
        }else if (originStatus==2){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待总经理/经理审批");
            ordersHistory.setBrief("订单材料信息提交");
        }else if (originStatus==5){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待安装师提交安装单");
            ordersHistory.setBrief("出货单提交");
        }else if (originStatus==6){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待财务确认收取结余费用");
            ordersHistory.setBrief("安装单提交");
        }
        else if (originStatus==7){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待尾款收取经理确认");
            ordersHistory.setBrief("尾款信息提交, 等待尾款确认");
        }
        else if (originStatus==8){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待最终确认");
            ordersHistory.setBrief("尾款信息确认, 等待最终确认");
        }
        else if (originStatus==9){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("订单完成");
            ordersHistory.setBrief("确认订单完成");
            orders.setStatus(0);
        }
        else if (originStatus==10){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待采购材料审批");
            ordersHistory.setBrief("提交采购材料信息");
            if (orders.getProcurementFile() == null){
                orders.setProcurementFile(orders.getMaterialFile());
            }
        }
        else if (originStatus==12){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("支付信息提交，等待材料");
            ordersHistory.setBrief("提交采购支付信息");
        }
        else if (originStatus==13){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("材料到货，等待加工出库");
            ordersHistory.setBrief("确认采购材料到货");
            orders.setStatus(5);
        }
        //更新订单信息
        ordersMapper.updateById(orders);
        SysUser sysUser = sysUserMapper.selectById(currentUserId);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();

        //添加订单操作的记录
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(orders.getId());
        ordersHistory.setOperate(0);
        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,ordersHistory.getOrderId())
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("操作成功");
    }

    @Override
    //删除订单
    public ResultData deleteOrder(String id) {
        ordersMapper.deleteById(id);
        LambdaQueryWrapper<OrdersHistory> history = new LambdaQueryWrapper<>();
        history.eq(OrdersHistory::getOrderId,id);
        ordersHistoryMapper.delete(history);
        return ResultData.success("订单删除成功");
    }

    @Override
    //新增订单
    public ResultData addOrSaveOrder(Orders orders) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, orders.getUsername());
        Long currentUserId = sysUserMapper.selectOne(wrapper).getId();
        //储存日期时间

        orders.setDealTime(orders.getDealTimeD());
        orders.setDealDate(orders.getDealDateD());


        Orders orders1 = ordersMapper.selectById(orders.getId());
        orders.setCreator(orders.getSeller());
        orders.setUpdater(orders.getSeller());
        //新增订单
        if (orders1 == null) {
            ordersMapper.insert(orders);
        }
        //更新订单
        else {
            orders.setUpdateDate(null);
            ordersMapper.updateById(orders);
        }
        //图片
        LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<Orders>();
        ordersWrapper.eq(Orders::getOrderId, orders.getOrderId());
        Long id = ordersMapper.selectOne(ordersWrapper).getId();

        LambdaQueryWrapper<PhotoInfo> contractPhotoWrapper = new LambdaQueryWrapper<PhotoInfo>();
        contractPhotoWrapper.eq(PhotoInfo::getOrderId, orders.getId()).eq(PhotoInfo::getPhotoType, PhotoType.ProductPhoto);
        photoInfoMapper.delete(contractPhotoWrapper);

        List<String> contractPhotos = orders.getContractPhotos();
        for (String contractPhoto : contractPhotos) {
            PhotoInfo photo = new PhotoInfo();
            photo.setOrderId(id);
            photo.setPhotoType(PhotoType.ContractPhoto);
            photo.setPhotoUrl(contractPhoto);
            photoInfoMapper.insert(photo);
        }
        //更新订单修改历史
        OrdersHistory ordersHistory = new OrdersHistory();
        SysUser sysUser = sysUserMapper.selectById(currentUserId);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("订单内容修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(orders.getId());
        ordersHistory.setOperate(1);
        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,ordersHistory.getOrderId())
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("订单信息更新成功");
    }

    @Override
    //获取所有订单
    public List<Orders> getOrders() {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getCreateDate);
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        for (Orders order : orders) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0800"));
            Long sellerId = order.getSeller();
            Long orderId = order.getId();

            //订单操作重要角色信息
            SysUser seller = sysUserMapper.selectById(sellerId);
            SysUser generalManager1 = sysUserMapper.selectById(order.getGeneralManager1());
            SysUser generalManager2 = sysUserMapper.selectById(order.getGeneralManager2());

            LambdaQueryWrapper<OrdersHistory> orderHistoryQueryWrapper = new LambdaQueryWrapper<>();
            orderHistoryQueryWrapper.eq(OrdersHistory::getOrderId, orderId);
            LambdaQueryWrapper<OrdersBoard> orderBoardQueryWrapper = new LambdaQueryWrapper<>();
            orderBoardQueryWrapper.eq(OrdersBoard::getOrderId, orderId);
            LambdaQueryWrapper<OrdersHardware> orderHardwareQueryWrapper = new LambdaQueryWrapper<>();
            orderHardwareQueryWrapper.eq(OrdersHardware::getOrderId, orderId);
            LambdaQueryWrapper<PhotoInfo> photoQueryWrapper = new LambdaQueryWrapper<>();
            photoQueryWrapper.eq(PhotoInfo::getOrderId, orderId);

            List<OrdersHistory> orderHistories = ordersHistoryMapper.selectList(orderHistoryQueryWrapper);
            List<OrdersBoard> ordersBoards = ordersBoardMapper.selectList(orderBoardQueryWrapper);
            List<OrdersHardware> ordersHardware = ordersHardwareMapper.selectList(orderHardwareQueryWrapper);
            List<PhotoInfo> photoInfos = photoInfoMapper.selectList(photoQueryWrapper);

            if(generalManager1!=null) {
                order.setGeneralManager1Name(generalManager1.getRealName());
            }
            if(generalManager2!=null) {
                order.setGeneralManager2Name(generalManager2.getRealName());
            }
            order.setOrderHistories(orderHistories);
            order.setOrdersBoards(ordersBoards);
            order.setOrdersHardware(ordersHardware);
            order.setSellerName(seller.getRealName());
            List<String> contractPhotos = new ArrayList<>();
            List<String> clientPhotos = new ArrayList<>();
            List<String> productPhotos = new ArrayList<>();
            List<String> signPhotos = new ArrayList<>();
            List<String> credentialPhotos = new ArrayList<>();
            List<String> paymentPhotos = new ArrayList<>();
            for(PhotoInfo photo : photoInfos) {
                if (PhotoType.ContractPhoto.equals(photo.getPhotoType())) {
                    contractPhotos.add(photo.getPhotoUrl());
                } else if (PhotoType.ClientPhoto.equals(photo.getPhotoType())) {
                    clientPhotos.add(photo.getPhotoUrl());
                } else if (PhotoType.ProductPhoto.equals(photo.getPhotoType())) {
                    productPhotos.add(photo.getPhotoUrl());
                } else if (PhotoType.SignPhoto.equals(photo.getPhotoType())) {
                    signPhotos.add(photo.getPhotoUrl());
                } else if (PhotoType.CredentialPhoto.equals(photo.getPhotoType())) {
                    credentialPhotos.add(photo.getPhotoUrl());
                } else if (PhotoType.PaymentPhoto.equals(photo.getPhotoType())) {
                    paymentPhotos.add(photo.getPhotoUrl());
                }
            }
            order.setContractPhotos(contractPhotos);
            order.setClientPhotos(clientPhotos);
            order.setProductPhotos(productPhotos);
            order.setSignPhotos(signPhotos);
            order.setCredentialPhotos(credentialPhotos);
            order.setPaymentPhotos(paymentPhotos);
        }
        return orders;
    }

    @Override
    //获取指定订单信息
    public Orders getOrder(Long id) {
        Orders order = ordersMapper.selectById(id);

        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0800"));
        //设置对应信息
        Long sellerId = order.getSeller();
        Long orderId = order.getId();
        SysUser seller = sysUserMapper.selectById(sellerId);
        SysUser generalManager1 = sysUserMapper.selectById(order.getGeneralManager1());
        SysUser generalManager2 = sysUserMapper.selectById(order.getGeneralManager2());
        //订单历史
        LambdaQueryWrapper<OrdersHistory> orderHistoryQueryWrapper = new LambdaQueryWrapper<>();
        orderHistoryQueryWrapper.eq(OrdersHistory::getOrderId, orderId);
        //订单板子
        LambdaQueryWrapper<OrdersBoard> orderBoardQueryWrapper = new LambdaQueryWrapper<>();
        orderBoardQueryWrapper.eq(OrdersBoard::getOrderId, orderId);
        //订单五金
        LambdaQueryWrapper<OrdersHardware> orderHardwareQueryWrapper = new LambdaQueryWrapper<>();
        orderHardwareQueryWrapper.eq(OrdersHardware::getOrderId, orderId);
        //订单出库板子单
        LambdaQueryWrapper<OutboundOrder> outboundBoardOrderWrapper = new LambdaQueryWrapper<>();
        outboundBoardOrderWrapper.eq(OutboundOrder::getOrderId, orderId).eq(OutboundOrder::getMaterialType, "板子");
        //订单出库五金单
        LambdaQueryWrapper<OutboundOrder> outboundHardwareOrderWrapper = new LambdaQueryWrapper<>();
        outboundHardwareOrderWrapper.eq(OutboundOrder::getOrderId, orderId).eq(OutboundOrder::getMaterialType, "五金");
        //出货单
        LambdaQueryWrapper<Shipment> shipmentWrapper = new LambdaQueryWrapper<>();
        shipmentWrapper.eq(Shipment::getOrderId, orderId);
        //图片
        LambdaQueryWrapper<PhotoInfo> photoQueryWrapper = new LambdaQueryWrapper<>();
        photoQueryWrapper.eq(PhotoInfo::getOrderId, orderId);

        List<OrdersHistory> orderHistories = ordersHistoryMapper.selectList(orderHistoryQueryWrapper);
        List<OrdersBoard> ordersBoards = ordersBoardMapper.selectList(orderBoardQueryWrapper);
        List<OrdersHardware> ordersHardware = ordersHardwareMapper.selectList(orderHardwareQueryWrapper);
        List<OutboundOrder> outboundBoardOrders = outboundOrderMapper.selectList(outboundBoardOrderWrapper);
        List<OutboundOrder> outboundHardwareOrders = outboundOrderMapper.selectList(outboundHardwareOrderWrapper);
        List<Shipment> shipments = shipmentMapper.selectList(shipmentWrapper);
        List<PhotoInfo> photoInfos = photoInfoMapper.selectList(photoQueryWrapper);

        List<String> contractPhotos = new ArrayList<>();
        List<String> clientPhotos = new ArrayList<>();
        List<String> productPhotos = new ArrayList<>();
        List<String> signPhotos = new ArrayList<>();
        List<String> credentialPhotos = new ArrayList<>();
        List<String> paymentPhotos = new ArrayList<>();
        if(generalManager1!=null) {
            order.setGeneralManager1Name(generalManager1.getRealName());
        }
        if(generalManager2!=null) {
            order.setGeneralManager2Name(generalManager2.getRealName());
        }
        order.setOrderHistories(orderHistories);
        order.setOrdersBoards(ordersBoards);
        order.setOrdersHardware(ordersHardware);
        order.setSellerName(seller.getRealName());
        order.setOutboundBoardOrders(outboundBoardOrders);
        order.setOutboundHardwareOrders(outboundHardwareOrders);
        order.setShipments(shipments);
        for(PhotoInfo photo : photoInfos) {
            if (PhotoType.ContractPhoto.equals(photo.getPhotoType())) {
                contractPhotos.add(photo.getPhotoUrl());
            } else if (PhotoType.ClientPhoto.equals(photo.getPhotoType())) {
                clientPhotos.add(photo.getPhotoUrl());
            } else if (PhotoType.ProductPhoto.equals(photo.getPhotoType())) {
                productPhotos.add(photo.getPhotoUrl());
            } else if (PhotoType.SignPhoto.equals(photo.getPhotoType())) {
                signPhotos.add(photo.getPhotoUrl());
            } else if (PhotoType.CredentialPhoto.equals(photo.getPhotoType())) {
                credentialPhotos.add(photo.getPhotoUrl());
            } else if (PhotoType.PaymentPhoto.equals(photo.getPhotoType())) {
                paymentPhotos.add(photo.getPhotoUrl());
            }
        }
        order.setContractPhotos(contractPhotos);
        order.setClientPhotos(clientPhotos);
        order.setProductPhotos(productPhotos);
        order.setSignPhotos(signPhotos);
        order.setCredentialPhotos(credentialPhotos);
        order.setPaymentPhotos(paymentPhotos);
        return order;
    }

    @Override
    //更新材料
    public ResultData updateOrderMaterial(Orders orders) {
        Long id = orders.getId();
        //板子
        List<OrdersBoard> ordersBoards = orders.getOrdersBoards();
        LambdaQueryWrapper<OrdersBoard> ordersBoardLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersBoardLambdaQueryWrapper.eq(OrdersBoard::getOrderId,id);
        ordersBoardMapper.delete(ordersBoardLambdaQueryWrapper);
        if(!ordersBoards.isEmpty()) {
            for (OrdersBoard ordersBoard: ordersBoards) {
                ordersBoard.setOrderId(id);
                //规范板子Title, 方便显示
                ordersBoard.setBoardTitle("板子: "+ordersBoard.getBoardType()+" "+ordersBoard.getBoardBase()+" "+ordersBoard.getBoardColor()+" | 数量："+ordersBoard.getBoardAmount());
                ordersBoardMapper.insert(ordersBoard);
            }
        }
        //五金
        List<OrdersHardware> ordersHardware = orders.getOrdersHardware();
        LambdaQueryWrapper<OrdersHardware> ordersHardwareLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHardwareLambdaQueryWrapper.eq(OrdersHardware::getOrderId,id);
        ordersHardwareMapper.delete(ordersHardwareLambdaQueryWrapper);
        for (OrdersHardware hardware: ordersHardware) {
            hardware.setOrderId(id);
            ordersHardwareMapper.insert(hardware);
        }
        //订单
        orders.setUpdateDate(null);
        ordersMapper.updateById(orders);
        //历史
        OrdersHistory ordersHistory = new OrdersHistory();
        //当前操作员
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, orders.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("订单材料内容修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);

        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,id)
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("订单材料信息更新成功");
    }

    @Override
    //更新支付信息
    public ResultData updatePayment(Orders orders) {
        Long id = orders.getId();
        //订单
        orders.setUpdateDate(null);
        ordersMapper.updateById(orders);
        //客户照片
        LambdaQueryWrapper<PhotoInfo> paymentPhotoWrapper = new LambdaQueryWrapper<PhotoInfo>();
        paymentPhotoWrapper.eq(PhotoInfo::getOrderId, orders.getId()).eq(PhotoInfo::getPhotoType, PhotoType.PaymentPhoto);
        photoInfoMapper.delete(paymentPhotoWrapper);

        List<String> paymentPhotos = orders.getPaymentPhotos();
        for (String paymentPhoto : paymentPhotos) {
            PhotoInfo photo = new PhotoInfo();
            photo.setOrderId(id);
            photo.setPhotoType(PhotoType.PaymentPhoto);
            photo.setPhotoUrl(paymentPhoto);
            photoInfoMapper.insert(photo);
        }

        //历史
        OrdersHistory ordersHistory = new OrdersHistory();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, orders.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("采购材料支付信息更新");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);

        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,id)
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("支付信息更新成功");
    }

    @Override
    //否决订单
    public ResultData denyOrder(Orders orders) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, orders.getUsername());
        Long currentUserId = sysUserMapper.selectOne(wrapper).getId();
        //订单信息更新
        Long id = orders.getId();
        Integer status = orders.getStatus();
        OrdersHistory ordersHistory = new OrdersHistory();
        if(status == 3){
            orders.setGeneralManager1(currentUserId);
            orders.setGeneralManager1Op("审批意见: 拒绝");
            ordersHistory.setBrief("订单审批意见：未通过");
            orders.setStatus(-1).setStatusInfo("订单审批未通过").setUpdater(null).setUpdateDate(null);
        }if(status == 4){
            orders.setGeneralManager2(currentUserId);
            orders.setGeneralManager2Op("审批意见: 拒绝");
            ordersHistory.setBrief("订单审批意见：未通过");
            orders.setStatus(-1).setStatusInfo("订单审批未通过").setUpdater(null).setUpdateDate(null);
        }if(status == 11){
            orders.setUpdater(currentUserId);
            orders.setStatusInfo("等待采购材料修改审批");
            ordersHistory.setBrief("材料采购信息退回");
            orders.setStatus(10).setUpdater(null).setUpdateDate(null);
        }
        /*Orders old = ordersMapper.selectById(orders.getId());
        orders.setGeneralManager1(old.getGeneralManager1());*/
        ordersMapper.updateById(orders);

        //历史
        SysUser sysUser = sysUserMapper.selectById(currentUserId);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);

        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,id)
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("订单否决成功");
    }

    @Override
    //通过订单
    public ResultData passOrder(Orders orders) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, orders.getUsername());
        Long currentUserId = sysUserMapper.selectOne(wrapper).getId();
        //订单信息更新
        Long id = orders.getId();
        Integer status = orders.getStatus();
        OrdersHistory ordersHistory = new OrdersHistory();
        if(status == 3){
            orders.setGeneralManager1(currentUserId);
            orders.setGeneralManager1Op("审批意见: 同意");
            orders.setStatusInfo("审批通过, 等待订单采购材料");
            ordersHistory.setBrief("订单审批意见：通过");
            orders.setStatus(10);
        }if(status == 4){
            orders.setGeneralManager2(currentUserId);
            orders.setGeneralManager2Op("审批意见: 同意");
            orders.setStatusInfo("审批通过, 等待订单采购材料");
            ordersHistory.setBrief("订单审批意见：通过");
            orders.setStatus(10);
        }if(status == 11){
            orders.setGeneralManager2(currentUserId);
            orders.setGeneralManager2Op("审批意见: 同意");
            orders.setStatusInfo("采购审批通过, 等待提交确认支付信息");
            ordersHistory.setBrief("采购审批意见：通过");
            orders.setStatus(12);
        }
        //orders.setStatus(status+1).setUpdater(currentUserId).setUpdateDate(null);
        orders.setUpdater(currentUserId).setUpdateDate(null);
        ordersMapper.updateById(orders);
        //历史
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, orders.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);
        ordersHistoryMapper.insert(ordersHistory);

        return ResultData.success("订单通过");
    }

    @Override
    //安装单
    public ResultData equipmentConfirm(Orders orders) {
        Long id = orders.getId();
        //订单
        orders.setUpdateDate(null);
        ordersMapper.updateById(orders);
        //客户照片
        LambdaQueryWrapper<PhotoInfo> clientPhotoWrapper = new LambdaQueryWrapper<PhotoInfo>();
        clientPhotoWrapper.eq(PhotoInfo::getOrderId, orders.getId()).eq(PhotoInfo::getPhotoType, PhotoType.ClientPhoto);
        photoInfoMapper.delete(clientPhotoWrapper);

        List<String> clientPhotos = orders.getClientPhotos();
        for (String clientPhoto : clientPhotos) {
            PhotoInfo photo = new PhotoInfo();
            photo.setOrderId(id);
            photo.setPhotoType(PhotoType.ClientPhoto);
            photo.setPhotoUrl(clientPhoto);
            photoInfoMapper.insert(photo);
        }

        //产品照片
        LambdaQueryWrapper<PhotoInfo> productPhotoWrapper = new LambdaQueryWrapper<PhotoInfo>();
        productPhotoWrapper.eq(PhotoInfo::getOrderId, orders.getId()).eq(PhotoInfo::getPhotoType, PhotoType.ProductPhoto);
        photoInfoMapper.delete(productPhotoWrapper);

        List<String> productPhotos = orders.getProductPhotos();
        for (String productPhoto : productPhotos) {
            PhotoInfo photo = new PhotoInfo();
            photo.setOrderId(id);
            photo.setPhotoType(PhotoType.ProductPhoto);
            photo.setPhotoUrl(productPhoto);
            photoInfoMapper.insert(photo);
        }


        //历史
        OrdersHistory ordersHistory = new OrdersHistory();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, orders.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("安装单内容修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);

        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,id)
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("安装单信息更新成功");
    }

    @Override
    //货物单
    public void shipmentConfirm(Orders orders) {
        /*Long id = orders.getId();
        OrdersHistory ordersHistory = new OrdersHistory();
        SysUser sysUser = sysUserMapper.selectById(orders.getDesigner());
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("安装单内容修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);

        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<OrdersHistory>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,id)
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }*/
    }

    //尾款单
    @Override
    public ResultData updateFinalPayment(Orders orders) {
        orders.setFinalPaymentDate(orders.getFinalPaymentDateD());
        Long id = orders.getId();
        //订单
        orders.setUpdateDate(null);
        ordersMapper.updateById(orders);

        //凭据照片
        LambdaQueryWrapper<PhotoInfo> credentialPhotoWrapper = new LambdaQueryWrapper<PhotoInfo>();
        credentialPhotoWrapper.eq(PhotoInfo::getOrderId, orders.getId()).eq(PhotoInfo::getPhotoType, PhotoType.CredentialPhoto);
        photoInfoMapper.delete(credentialPhotoWrapper);

        List<String> credentialPhotos = orders.getCredentialPhotos();
        for (String productPhoto : credentialPhotos) {
            PhotoInfo photo = new PhotoInfo();
            photo.setOrderId(id);
            photo.setPhotoType(PhotoType.CredentialPhoto);
            photo.setPhotoUrl(productPhoto);
            photoInfoMapper.insert(photo);
        }


        //历史
        OrdersHistory ordersHistory = new OrdersHistory();
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUsername, orders.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        String name = sysDeptMapper.selectById(sysUser.getDeptId()).getName();
        ordersHistory.setBrief("财务尾款内容修改");
        ordersHistory.setOperator(sysUser.getRealName()+"("+name+")");
        ordersHistory.setOrderId(id);
        ordersHistory.setOperate(1);

        LambdaQueryWrapper<OrdersHistory> ordersHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersHistoryLambdaQueryWrapper.eq(OrdersHistory::getOrderId,id)
                .eq(OrdersHistory::getOperate, ordersHistory.getOperate())
                .eq(OrdersHistory::getBrief, ordersHistory.getBrief());
        OrdersHistory ordersHistory1 = ordersHistoryMapper.selectOne(ordersHistoryLambdaQueryWrapper);
        if(ordersHistory1 == null){
            ordersHistoryMapper.insert(ordersHistory);
        }else {
            ordersHistoryMapper.update(ordersHistory,ordersHistoryLambdaQueryWrapper);
        }
        return ResultData.success("财务尾款信息更新成功");
    }
}
